import axios from 'axios';
import { ElMessage } from 'element-plus';

class HttpRequest {
  constructor(baseUrl = baseUrl) {
    this.baseUrl = baseUrl;
  }
  getInsideConfig() {
    const config = {
      baseURL: this.baseUrl,
      headers: {
        // access_token
        // Authorization: '',
      },
    };
    return config;
  }
  interceptors(instance) {
    // 请求拦截
    instance.interceptors.request.use(
      (config) => config,
      (error) => {
        return Promise.reject(error);
      }
    );
    // 响应拦截
    instance.interceptors.response.use(
      (res) => {
        const { data } = res;
        if (data?.error) {
          ElMessage.error(data.message);
          return Promise.reject(data.message);
        }
        return data;
      },
      (error) => {
        // console.log(error);
        ElMessage.error(error.message);
        return Promise.reject(error);
      }
    );
  }
  request(options) {
    const instance = axios.create();
    options = Object.assign(this.getInsideConfig(), options);
    this.interceptors(instance);
    return instance(options);
  }
}
export default HttpRequest;
